'use strict';

/**
 * 菜单控制器
 */
const Controller = require('egg').Controller;

class MenuController extends Controller {
  /**
   * 创建菜单
   * @return {Promise<void>}
   */
  async create() {
    const { ctx, service } = this;
    const menuData = ctx.request.body;
    const result = await service.menu.create(menuData);
    ctx.success(result);
  }

  /**
   * 查询菜单列表
   * @return {Promise<void>}
   */
  async index() {
    const { ctx, service } = this;
    const { page = 1, pageSize = 10, ...query } = ctx.query;
    const result = await service.menu.list(query, Number(page), Number(pageSize));
    ctx.success(result);
  }

  /**
   * 查询菜单树形结构
   * @return {Promise<void>}
   */
  async tree() {
    const { ctx, service } = this;
    const result = await service.menu.tree(ctx.query);
    ctx.success(result);
  }

  /**
   * 查询单个菜单
   * @return {Promise<void>}
   */
  async show() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const result = await service.menu.findById(id);
    ctx.success(result);
  }

  /**
   * 更新菜单
   * @return {Promise<void>}
   */
  async update() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const menuData = ctx.request.body;
    const result = await service.menu.update(id, menuData);
    ctx.success(result);
  }

  /**
   * 删除菜单
   * @return {Promise<void>}
   */
  async destroy() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    await service.menu.destroy(id);
    ctx.success(null, '删除成功');
  }

  /**
   * 更新菜单状态
   * @return {Promise<void>}
   */
  async updateStatus() {
    const { ctx, service } = this;
    const { id, status } = ctx.request.body;
    await service.menu.updateStatus(id, status);
    ctx.success(null, '状态更新成功');
  }

  /**
   * 根据角色编码获取菜单列表
   * @return {Promise<void>}
   */
  async getMenusByRoleCode() {
    const { ctx, service } = this;
    const { roleCode } = ctx.params;
    const result = await service.menu.getMenusByRoleCode(roleCode);
    ctx.success(result);
  }

  /**
   * 根据角色编码获取菜单树形结构
   * @return {Promise<void>}
   */
  async getMenuTreeByRoleCode() {
    const { ctx, service } = this;
    const { roleCode } = ctx.params;
    const result = await service.menu.getMenuTreeByRoleCode(roleCode);
    ctx.success(result);
  }

  /**
   * 为角色分配菜单
   * @return {Promise<void>}
   */
  async assignMenusToRole() {
    const { ctx, service } = this;
    const { roleCode, menuIds } = ctx.request.body;
    await service.menu.assignMenusToRoleCode(roleCode, menuIds);
    ctx.success(null, '分配成功');
  }

  /**
   * 获取用户菜单列表
   * @return {Promise<void>}
   */
  async getUserMenus() {
    const { ctx, service } = this;
    const { userId } = ctx.params;
    const result = await service.menu.getUserMenus(userId);
    ctx.success(result);
  }

  /**
   * 获取用户菜单树形结构
   * @return {Promise<void>}
   */
  async getUserMenuTree() {
    const { ctx, service } = this;
    const { userId } = ctx.params;
    const result = await service.menu.getUserMenuTree(userId);
    ctx.success(result);
  }
}

module.exports = MenuController;